package com.regiee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regiee.common.R;
import com.regiee.dto.DishDto;
import com.regiee.entity.Category;
import com.regiee.entity.Dish;
import com.regiee.service.CategoryService;
import com.regiee.service.DishFlavorService;
import com.regiee.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜品管理
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    /*
    新增菜品接口
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveFlavor(dishDto);
        return R.success("新增成功");
    }
    /*
    菜品分页查询接口
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(int page, int pageSize, String name){
        //分页对象
        Page<Dish> pageInfo=new Page<>(page,pageSize);
        Page<DishDto> pageDtoInfo=new Page<>();
        //创建分页条件
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<Dish>()
                .like(StringUtils.isNotEmpty(name),Dish::getName,name)
                .orderByDesc(Dish::getUpdateTime);
        //进行查询
        dishService.page(pageInfo,wrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,pageDtoInfo,"records");//这里records是除了records不拷贝其他的都要拷贝
        List<Dish> list=pageInfo.getRecords();
        List<DishDto> dtoList=new ArrayList<>();
        for(Dish dish:list){
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            Long categoryId=dish.getCategoryId();//分类Id
            String categoryName=categoryService.getById(categoryId).getName();//分类的名称
            dishDto.setCategoryName(categoryName);
            dtoList.add(dishDto);
        }
        pageDtoInfo.setRecords(dtoList);
        return R.success(pageDtoInfo);
    }
    /*
    根据Id查询菜品和口味接口
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
       DishDto dishDto= dishService.getByFlavor(id);
        return R.success(dishDto);
    }
    /*
    修改菜品Id
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.uodateFlavor(dishDto);
        return R.success("修改成功");
    }
    /*
    批量停售.单个停售
     */
    @PostMapping("/status/0")
    public R<String> UpdateStatus(Long[] ids){
        Long[] id=ids;
        if(id.length==1){
            Dish dish=dishService.getById(id[0]);
            dish.setStatus(0);
            dishService.updateById(dish);
            return R.success("修改成功");
        }
        List<Long> list=new ArrayList<>();
        for(int i=0;i<id.length;i++){
            list.add(i,id[i]);
        }
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<Dish>()
                .in(Dish::getId, list);
        List<Dish> list1=dishService.list(wrapper);
        for(Dish dish:list1){
            dish.setStatus(0);
        }
        dishService.updateBatchById(list1);
        return R.success("修改成功");
    }
    /*
    批量启售.单个启售
     */
    @PostMapping("/status/1")
    public R<String> AllUpdateStatus(Long[] ids){
        Long[] id=ids;
        if(id.length==1){
                Dish dish=dishService.getById(id[0]);
                dish.setStatus(1);
                dishService.updateById(dish);
                return R.success("修改成功");
        }
        List<Long> list=new ArrayList<>();
        for(int i=0;i<id.length;i++){
            list.add(i,id[i]);
        }
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<Dish>()
                .in(Dish::getId, list);
        List<Dish> list1=dishService.list(wrapper);
        for(Dish dish:list1){
            dish.setStatus(1);
        }
        dishService.updateBatchById(list1);
        return R.success("修改成功");
    }
    /*
    删除菜品、批量删除
     */
    @DeleteMapping
    public R<String> delete(Long[] ids){
        Long[] id=ids;
        if(id.length==1){
            dishService.removeById(id[0]);
            return R.success("删除成功");
        }
        List<Long> list=new ArrayList<>();
        for(int i=0;i<id.length;i++){
            list.add(i,id[i]);
        }
        dishService.removeBatchByIds(list);
        return R.success("删除成功");
    }
    /*
    查询菜品，用于套餐管理模块
     */
    @GetMapping("/list")
    public R<List<Dish>> getDish(Dish dish){
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<Dish>()
                .eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId())
                .eq(Dish::getStatus,1)//只查询状态为1的菜品
                .orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list=dishService.list(wrapper);
        return R.success(list);
    }
}
